using System.IO;

public class UDPMessageBase
{
	public enum MessageType
	{
		ServerDiscoveryMessage = 0,
		ServerIntroductionMessage = 1,
		ServerReject = 2,
		HeartbeatMessage = 3,
		RequestConnection = 4,
		ConnectionReject = 5,
		ControllerDataMessage = 6
	}

	public MessageType type;

	public int version;

	public int receiverId;

	public int senderId;

	public string app;

	public GGIPAddress senderAddress;

	public int senderPort;

	public byte[] data;

	public GGUDPNetworkServer server;

	public T GetMessage<T>() where T : UDPMessageBase, new()
	{
		T result = new T();
		result.DeserializeFromBytes(data);
		return result;
	}

	public virtual byte[] SerializeToBytes()
	{
		MemoryStream memoryStream = new MemoryStream();
		BinaryWriter writer = new BinaryWriter(memoryStream);
		Serialize(writer);
		return memoryStream.ToArray();
	}

	public virtual void Serialize(BinaryWriter writer)
	{
		writer.Write((int)type);
		writer.Write(version);
		writer.Write(receiverId);
		writer.Write(senderId);
		writer.Write(GGServerConstants.instance.appName);
	}

	public virtual void DeserializeFromBytes(byte[] data)
	{
		MemoryStream input = new MemoryStream(data);
		BinaryReader reader = new BinaryReader(input);
		Deserialize(reader);
		this.data = data;
	}

	public virtual void Deserialize(BinaryReader reader)
	{
		type = (MessageType)reader.ReadInt32();
		version = reader.ReadInt32();
		receiverId = reader.ReadInt32();
		senderId = reader.ReadInt32();
		app = reader.ReadString();
	}
}
